import {
  get,
  postParam
} from "../../utils/service"
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //swiper设置
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    detail: {},
    widht: wx.getSystemInfoSync().windowWidth,
    sheetShow: false,
    radioValue: null,

    tableHeader: [],
    stripe: true,
    border: true,
    outBorder: true,
    row: [],
    msg: '暂无数据'
  },
  sheetToShow: function () {
    this.setData({
      sheetShow: true
    })
  },
  sheetToClose: function () {
    this.setData({
      sheetShow: false
    })
  },
  onRadioChange: function (event) {
    this.setData({
      radioValue: event.detail,
    });
  },
  onCellClick: function (event) {
    const {
      name
    } = event.currentTarget.dataset
    this.setData({
      radioValue: name,
    })
  },
  //提交创建订单
  //filesId参数暂时未使用,如果后续有自定义套餐时,可能会使用
  onActionSheetCancel: function () {
    const toast = Toast.loading({
      message: '订单创建中...',
      forbidClick: true,
    })
    postParam(`${app.globalData.host}/order/create`, {
      comboId: this.data.radioValue,
      clothingId: this.data.detail.id
    }, {
      success: function (data) {
        app.globalData.orderId = data.id
        wx.navigateTo({
          url: `/pages/order/order?orderId=${data.id}`,
        })
      },
      complete: function () {
        toast.clear()
      }
    })
  },
  onCollect: function (e) {
    const clothingId = this.data.detail.id
    postParam(`${app.globalData.host}/favorite/${clothingId}`, {}, {
      success: function () {
        Notify({
          type: 'success',
          message: '收藏成功'
        })
      },
      fail: function (err) {
        Notify({
          type: 'danger',
          message: err.message
        })
      }
    })
  },
  getSizeParams: function (id) {
    const self = this
    get(`${app.globalData.host}/clothingSize/${id}`, {}, {
      success: self.decodeSizeParams
    })
  },
  decodeSizeParams: function (data) {
    console.info("================== 尺码表信息 ==============", data)
    const {
      sizeTableParam,
      sizeTables
    } = data
    const ph = (sizeTableParam || []).filter(sp => sp.level === 'primary' || sp.level === 'require')
      .map(sp => ({
        prop: sp.columnName.toLowerCase(),
        label: sp.displayName,
        width: 100
      }))
    const header = [{
      prop: 'a',
      width: 150,
      color: '#55C355',
      label: '部位/规格',
    }].concat(ph)
    this.setData({
      tableHeader: header,
      row: sizeTables
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const width = wx.getSystemInfoSync().windowWidth
    const height = width * (618 / 400)
    this.setData({
      height: height,
      width: width,
      isFree: app.globalData.isFree
    })
    const id = options.id
    const self = this
    self.getSizeParams(id)
    get(`${app.globalData.host}/clothing/${id}`, {}, {
      success: function (data) {
        console.info("==detail==", data)
        data.combos.forEach(e => {
          if (e.includeSize) {
            e.includeSizes = e.includeSize.split(',')
          }
          const isFree = app.globalData.isFree
          console.info("======= 是否免费 =======", isFree)
          //TODO:判断是否为会员
          if (isFree) {
            e.price = '免费'
          } else {
            e.price = (e.price / 100).toFixed(2)
          }
        })
        self.setData({
          detail: data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})